package com.framework.web.sys.controller;


import com.framework.core.constant.Globals;
import com.framework.core.domain.AjaxJson;
import com.framework.core.util.MyBeanUtils;
import com.framework.core.util.StringUtil;
import com.framework.web.common.mappings.SysUrlMappings;
import com.framework.web.sys.domain.Resource;
import com.framework.web.sys.domain.RoleResource;
import com.framework.web.sys.service.ResourceService;
import com.framework.web.sys.service.RoleResourceService;
import com.framework.web.sys.vo.ResourceFastVo;
import com.framework.web.sys.vo.ResourceVo;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 资源信息管理——控制层
 *
 * @author fusf
 */
@Controller
public class ResourceController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());


    /**
     * Spring注入资源信息模块service
     */
    @Autowired
    private ResourceService resourceService;

    @Autowired
    private RoleResourceService roleResourceService;


    /**
     * 资源列表
     *
     * @param resource
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = SysUrlMappings.SYS_RESOURCE_LIST, method = RequestMethod.POST)
    @ResponseBody
    public List<ResourceVo> list(Resource resource, HttpServletRequest request) throws Exception {
        List<Resource> resourceList = resourceService.findListOrderBySortIndex();
        List<ResourceVo> resourceVOList = new ArrayList<ResourceVo>();
        for (Resource res : resourceList) {
            ResourceVo resVo = new ResourceVo();
            MyBeanUtils.copyBeanNotNull2Bean(res, resVo);
            resourceVOList.add(resVo);
        }
        return ResourceVo.getRoot(resourceVOList);
    }


    /**
     * 资源信息模块添加、修改页面
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = SysUrlMappings.SYS_RESOURCE_TOSAVEANDUPDATE, method = RequestMethod.GET)
    public ModelAndView toSaveAndUpdate(@PathVariable("id") String id, @PathVariable("parentId") String parentId)
            throws Exception {
        // 进入资源信息模块添加或修改页面
        ModelAndView modelAndView = new ModelAndView(SysUrlMappings.V_SYS_RESOURCE_TOSAVEANDUPDATE);
        Resource resource = new Resource();
        if (StringUtil.isNotEmpty(id) && !"0".equals(id)) {
            resource = resourceService.findById(id);
        } else {
            // 初始化默认状态
            resource.setStatus(Globals.STATUS_YES);
            resource.setResourceType(Globals.RES_TYPE_AUTHORITY);//默认是按钮权限
            resource.setSortIndex(0);
            if (StringUtil.isNotEmpty(parentId) && !"0".equals(parentId)) {
                resource.setParentId(parentId);//父类Id
            }
        }
        modelAndView.addObject("resource", resource);
        return modelAndView;
    }


    /**
     * 添加或修改组资源信息构实体
     *
     * @param resource 资源信息实体
     * @return
     * @throws Exception
     */
    @RequestMapping(value = SysUrlMappings.SYS_RESOURCE_SAVEANDUPDATE, method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson saveAndUpdate(HttpServletRequest request, HttpServletResponse response, Resource resource) throws Exception {
        AjaxJson j = new AjaxJson();
        if (StringUtils.isNotEmpty(resource.getId())) {//更新
            if (resource.getId().equals(resource.getParentId())) {
                j.setSuccess(false);
                j.setMsg("上级资源不能选择自身");
                return j;
            }
            Resource r = resourceService.findById(resource.getId());
            MyBeanUtils.copyBeanNotNull2Bean(resource, r);
            resource = r;
        }
        resource.setStatus(Globals.USER_NORMAL);
        resourceService.save(resource);
        j.setMsg("资源信息保存成功");
        return j;
    }

    /**
     * 根据id字符串删除指定资源信息数据
     *
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = SysUrlMappings.SYS_RESOURCE_DELETE, method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson delete(String id) throws Exception {
        AjaxJson j = new AjaxJson();
        resourceService.delete(id);
        j.setMsg("删除成功");
        return j;
    }

    /**
     * 获取资源信息树json数据
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = SysUrlMappings.SYS_RESOURCE_COMMONTREE)
    @ResponseBody
    public Object commonTree(HttpServletRequest request) throws Exception {
        List<Resource> resourceList = resourceService.findListOrderBySortIndex();
        return resourceService.findTreeResource(resourceList);
    }

    /**
     * 角色对应的资源列表
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = SysUrlMappings.SYS_RESOURCE_COMMONTREE_BYROLEID)
    @ResponseBody
    public Object commonTreeByRoleId(HttpServletRequest request, String roleId) throws Exception {
        List<Resource> resourceList = resourceService.findListByStatusOrderBySortIndex();
        List<RoleResource> roleResourceList = roleResourceService.findByRoleId(roleId);
        return resourceService.findTreeResource(resourceList, roleResourceList);
    }

    /**
     * 保存角色资源关联关系
     *
     * @param request
     * @param response
     * @param roleId
     * @param resourceIds
     * @return
     * @throws Exception
     */
    @RequestMapping(value = SysUrlMappings.SYS_ROLE_RESOURCE_SAVEANDUPDATE, method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson saveAndUpdate(HttpServletRequest request, HttpServletResponse response, String roleId, String resourceIds) throws Exception {
        AjaxJson j = new AjaxJson();
        roleResourceService.save(roleId, resourceIds);
        return j;
    }

    /**
     * 资源信息模块，快速生成配置跳转页面
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = SysUrlMappings.SYS_RESOURCE_FAST_TOSAVE, method = RequestMethod.GET)
    public ModelAndView toFastSave(@PathVariable("id") String id, @PathVariable("parentId") String parentId)
            throws Exception {
        ModelAndView modelAndView = new ModelAndView(SysUrlMappings.V_SYS_RESOURCE_FAST_TOSAVEA);
        Resource resource = new Resource();
        if (StringUtil.isNotEmpty(parentId) && !"0".equals(parentId)) {
            resource.setParentId(parentId);//父类Id
        }
        modelAndView.addObject("resource", resource);
        return modelAndView;
    }

    /**
     *
     * 快速生成资源配置文件
     * @param resource 资源信息实体
     * @return
     * @throws Exception
     */
    @RequestMapping(value = SysUrlMappings.SYS_RESOURCE_FAST_SAVE, method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson saveFast(HttpServletRequest request, HttpServletResponse response, ResourceFastVo resource) throws Exception {
        AjaxJson j = new AjaxJson();
        resourceService.createFastResource(resource);
        j.setMsg("资源信息快速生成成功");
        return j;
    }


}